import { getImage, getPage } from '../../utils/config';
import { http, promisify } from '../../utils/utils';
import api from '../../utils/api';
import wxApi from '../../utils/wxApi';
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        loading: 0,
        getImage: getImage,
        getPage: getPage,
        filterId: 1,
        filterType: 0,
        matrixType: 0,
        navbar: {},
        window: {},
        cid: 0,
        keyword: '',
        pageSize: 20,
        pageIndex: 1,
        nextPage: true,
        orders: 'desc',
        shopNo: '',
        loadmore: false,
        loaddone: false,
        abnor: false,
    },

    /**
     * 返回上一页
     */
    goBackTap() {
        wxApi.navigateBack(1);
    },

    /**
     * 搜索提交
     */
    submitSearch(e) {
        const keyword = e.detail;
        //如果不全是空格则执行
        if (!keyword.match(/^\s*$/)) {
            this.setData({
                keyword: keyword,
                productsInfo: [],
                pageIndex: 1,
                nextPage: true,
                loadmore: false,
                loaddone: false,
                abnor: false,
                loading: 0,
            });

            let { pageSize, loaddone, abnor } = this.data;

            //更新商品数据
            this.getShopProductsInfo((data) => {
                //判断是否有数据
                if (data.list.length == 0) abnor = true;
                //判断是否加载完
                if (data.list.length < pageSize) loaddone = true;
                this.setData({ productsInfo: data.list, loading: 1, loaddone, abnor });
            });
        }
    },

    /**
     * 切换风格
     */
    tabsMatrixTap() {
        let { matrixType } = this.data;
        matrixType == 0 ? matrixType = 1 : matrixType = 0;
        this.setData({  matrixType });
    },

    /**
     * 筛选产品
     */
    tabsFilterTap(e) {
        const { dataset } = e.currentTarget || e.target;
        let { filterType, filterId } = this.data;
        let type, typeText;

        if (dataset.id != 3 && filterId == dataset.id) return false;

        if (dataset.id == 3) {
            switch (filterType) {
                case 0:
                    type = 1;
                    typeText = 'asc';
                break;
                case 1:
                    type = 2;
                    typeText = 'desc';
                break;
                case 2:
                    type = 1;
                    typeText = 'asc';
                break;
            }

            this.setData({
                filterType: type,
                orders: typeText,
            })
        } else {
            this.setData({
                filterType: 0,
                orders: 'desc',
            });
        }

        this.setData({
            filterId: dataset.id,
            productsInfo: [],
            pageIndex: 1,
            nextPage: true,
            loadmore: false,
            loaddone: false,
            abnor: false,
            loading: 0,
        });

        let { pageSize, loaddone, abnor } = this.data;

        //更新商品数据
        this.getShopProductsInfo((data) => {
            //判断是否有数据
            if (data.list.length == 0) abnor = true;
            //判断是否加载完
            if (data.list.length < pageSize) loaddone = true;
            this.setData({ productsInfo: data.list, loading: 1, loaddone, abnor });
        });
    },

    /**
     * 获取商品--网络请求
     */
    getShopProductsInfo(cb, a = false) {
        let { shopNo, pageSize, keyword, cid, pageIndex, nextPage, abnor, filterId, orders } = this.data;

        http.get(api.default, {
            data: {
                action: 'getstoresproducts',
                cid: cid,
                keyword: keyword,
                pagesize: pageSize,
                pageindex: pageIndex,
                shopno: shopNo,
                ordersid: filterId,
                orders: orders,
            }
        }, a).then((res) => {
            const { data } = res.data;

            //判断是否还有下一页
            if (data.list.length < pageSize) nextPage = false;
            this.setData({ nextPage });

            cb && cb(data);
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        if(JSON.stringify(options) !== '{}'){
            this.setData({
                shopNo: options.id,
                cid: options.cid,
            })
        }

        //获取system信息
        promisify(wx.getSystemInfo)().then(res => {
            //获取右上角胶囊信息
            this.setData({
                navbar: wx.getMenuButtonBoundingClientRect(),
                window: res,
            })
        });

        let { pageSize, loaddone, abnor } = this.data;
        this.getShopProductsInfo((data) => {

            //判断是否有数据
            if (data.list.length == 0) abnor = true;
            //判断是否加载完
            if (data.list.length < pageSize) loaddone = true;
            this.setData({ productsInfo: data.list, loading: 1, loaddone, abnor });
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let { pageIndex, nextPage, loadmore } = this.data;

        if (nextPage && !loadmore) {
            pageIndex++;
            loadmore = true;

            this.setData({ pageIndex, loadmore });

            this.getShopProductsInfo((data) => {
                let { productsInfo, pageSize, loaddone } = this.data;

                loadmore = false;

                //判断是否加载完
                if (data.list.length < pageSize) loaddone = true;

                productsInfo = productsInfo.concat(data.list);

                this.setData({ productsInfo, loadmore, loaddone });
            })
        }
    }
})